package com.fanco.learn_datastruct_leetcode.designpattern.singleton.type3;

/**
 * @author qiaowang 2022/2/9
 * 描述:懒汉式（线程不安全）
 * 优点：起到了懒加载的作用，但是只能在单线程下使用
 * 缺点：多线程下，会由于竞态条件导致产生了多个实例
 */
public class Singleton03 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance1 == instance);
        System.out.println(instance1.hashCode());
        System.out.println(instance.hashCode());
    }
}

class Singleton {
    private static Singleton instance;

    private Singleton() {
    }

    // 静态方法获取实例数据
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}
